<%-- 
    Document   : confirmacaoMateriais
    Created on : 09/03/2014, 19:41:56
    Author     : jordan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Confirma Material</title>
    </head>
    <body>
        <c:if test="${param.retorna=='retorna'||param.confExclui=='nao'}">
            <c:redirect url="pesquisaMateriais.jsp"/>
        </c:if>
        <h1>Confirma Material</h1>
        <%boolean materialToxico = false;%>
        <c:if test="${param.toxico=='on'}">
            verdadeiro<br>
            <%materialToxico=true;%>
        </c:if>
        <c:if test="${param.toxico !='on'}">
            falso<br>
            <%materialToxico=false;%>
        </c:if>
        <jsp:useBean id="material" class="beans.Material" scope="request"/>
        <jsp:setProperty name="material" property="id" param="id"/>
        <jsp:setProperty name="material" property="descricao" param="descricao"/>
        <jsp:setProperty name="material" property="tempoDecomposicao" param="tempoDecomposicao"/>
        <jsp:setProperty name="material" property="toxico" param="toxico"/>
        <jsp:setProperty name="material" property="tipo" param="tipo"/>
        <jsp:setProperty name="material" property="observacoes" param="observacoes"/>
        <c:set var="msgError" value=""/>
        <%--                  Perguntar para Anna
        <c:redirect url="pesquisaMateriais.jsp?er=${material.erro}"/>
        --%>
        <c:if test="${param.operacao=='insere'}">
            <c:set var="retornoOperacao" value="insere=insere"/>
        </c:if>
        <c:if test="${param.operacao=='altera'}">
            <c:set var="retornoOperacao" value="altera=altera&id=${param.id}"/>
        </c:if>
        <c:set var="testeDescricao" value="<%=String.valueOf(request.getParameter("descricao")).trim()%>"/>
        <c:if test="${empty(testeDescricao)}">
            <c:set var="msgError" value="${msgError}${'26,E;'}"/>
        </c:if>
        <c:set var="testeTempoDecomposicao" value="<%=String.valueOf(request.getParameter("tempoDecomposicao")).trim()%>"/>
        <c:if test="${empty(testeTempoDecomposicao)}">
            <c:set var="msgError" value="${msgError}${'27,E;'}"/>
        </c:if>
        <c:set var="testeTipo" value="<%=String.valueOf(request.getParameter("tipo")).trim()%>"/>
        <c:if test="${empty(testeTipo)}">
            <c:set var="msgError" value="${msgError}${'28,E;'}"/>
        </c:if>

        <c:if test="${!empty(msgError)}">
            <c:redirect url="materiais.jsp?error=${msgError}&${retornoOperacao}"/>
        </c:if>
        
        
        
        <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
                           url="jdbc:mysql://localhost/coleta_seletiva"
                           user="root"/>
        <c:if test="${param.confExclui=='sim'}">
            <sql:update dataSource="${snapshot}" var="count">
                DELETE FROM material WHERE id = ?;
                <sql:param value="${material.id}"/>
             </sql:update>   
            <c:if test="${count <= 0}">
                <c:set var="msgError" value="${msgError}${'1,E;'}"/>
            </c:if>
            <c:if test="${count > 0}">
                <c:set var="msgError" value="${msgError}${'2,S;'}"/>
            </c:if>
            <c:redirect url="pesquisaMateriais.jsp?error=${msgError}"/>
        </c:if>
        <c:if test="${param.operacao=='insere'}">
            <sql:update dataSource="${snapshot}" var="count">
                INSERT INTO material (descricao, tempo_decomposicao, toxico, tipo, observacoes) VALUES (?, ?, ?, ?, ?);
                <sql:param value="${material.descricao}"/>
                <sql:param value="${material.tempoDecomposicao}"/>
                <sql:param value="${material.toxico}"/>
                <sql:param value="${material.tipo}"/>
                <sql:param value="${material.observacoes}"/>
             </sql:update>   
            <c:if test="${count <= 0}">
                <c:set var="msgError" value="${msgError}${'3,E;'}"/>
            </c:if>
            <c:if test="${count > 0}">
                <c:set var="msgError" value="${msgError}${'4,S;'}"/>
            </c:if>
            <c:redirect url="materiais.jsp?error=${msgError}&${retornoOperacao}"/>
        </c:if>
        <c:if test="${param.operacao=='altera'}">
            <sql:update dataSource="${snapshot}" var="count">
                UPDATE  material
                    SET descricao = ?, 
                        tempo_decomposicao = ?, 
                        toxico = ?, 
                        tipo = ?, 
                        observacoes = ?
                    WHERE id = ?;
                <sql:param value="${material.descricao}"/>
                <sql:param value="${material.tempoDecomposicao}"/>
                <c:if test="${material.toxico}">
                    <sql:param value="<%=materialToxico%>"/>
                </c:if>
                <c:if test="${!material.toxico}">
                    <sql:param value="<%=materialToxico%>"/>
                </c:if>
                <%--
                <c:if test="${material.toxico}">
                    <sql:param value="1"/>
                </c:if>
                <c:if test="${!material.toxico}">
                </c:if>
                <sql:param value="<%=studentId%>" />
                <sql:param value="<%=materialToxico%>"/>
                <sql:param value="${material.toxico}"/>
                --%>
                <sql:param value="${material.tipo}"/>
                <sql:param value="${material.observacoes}"/>
                <sql:param value="${material.id}"/>
             </sql:update>   
            <c:if test="${count <= 0}">
                <c:set var="msgError" value="${msgError}${'5,E;'}"/>
            </c:if>
            <c:if test="${count > 0}">
                <c:set var="msgError" value="${msgError}${'6,S;'}"/>
            </c:if>
            <c:redirect url="pesquisaMateriais.jsp?error=${msgError}"/>
        </c:if>
    </body>
</html>
